package com.employees.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.employees.bean.Employee;
import com.employees.bean.EmployeeBase;
import com.employees.bean.Employeeinfo;
import com.employees.bean.Employeeskill;
import com.employees.bean.PageUtil;
import com.employees.bean.Pager;
import com.employees.bean.Result;
import com.employees.dao.EmployeeDao;
import com.employees.dao.impl.EmployeeDaoImpl;
import com.employees.service.EmployeeService;

public  class EmployeeServiceImpl implements EmployeeService {

	private EmployeeDao employeeDao;

	private Pager pager;
	
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	public List findAllEmployee() {
		// TODO Auto-generated method stub
		return employeeDao.findAllEmployee();
	}

	public Employee getEmployee(String no, String name) {
		// TODO Auto-generated method stub
		return employeeDao.getEmployee(no, name);
	}

	public void addEmployee(Object employee) {
		// TODO Auto-generated method stub
		employeeDao.addEmployee(employee);
	}

	
	public String getHql(String name, String depart,
			String sex) {
		// TODO Auto-generated method stub
		return employeeDao.getHql(name, depart, sex);
	}

	@Override
	public Result findByPage(Pager pager ,String hql) {
		
		int totalRows=employeeDao.getRows();
		pager=PageUtil.createPage(pager, totalRows);
		List list =employeeDao.findWithPage(pager,hql);
		List arrlist = new ArrayList();
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Object[] o = (Object[]) iterator.next();

			Employee employee1 = (Employee) o[0];
			Employeeinfo employeeinfo = (Employeeinfo) o[1];
			Employeeskill employeeskill = (Employeeskill) o[2];
			EmployeeBase base = new EmployeeBase();
			
			base.setSkillId(employeeskill.getId());
			base.setInfoId(employeeinfo.getId());
			base.setEmployeeNo(employee1.getNo().toString());
			base.setEmployeeName(employee1.getName().toString());
			base.setEmployeeBirthday(employeeinfo.getBirth().toString().substring(0,
					10));
			base.setEmployeeSex(employeeinfo.getSex().toString());
			base.setEmployeeDepartment(employeeinfo.getDepartment().toString());
			//base.setPhoto(employeeinfo.getPhoto());
			
			base.setEmployeeSkill(employeeskill.getSkill());
			
			arrlist.add(base);	
	}
		return new Result(pager,arrlist);
	}

	@Override
	public void updateEmployee(Object employee) {
		// TODO Auto-generated method stub
		employeeDao.updateEmployee(employee);
	}
	
	public void deleteEmployee(Object employee){
		employeeDao.deleteEmployee(employee);
	}

	@Override
	public Employeeinfo getImage(int id) {
		// TODO Auto-generated method stub
		return employeeDao.getImage(id);
	}
}
